package com.example.androidtset;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout main_home;
    private LinearLayout main_address;
    private LinearLayout main_find;
    private LinearLayout main_i;

    private HomeFragment homeFragment;
    private AddressFragment addressFragment;
    private ActivityFragment findFragment;
    private IFragment iFragment;

    private FragmentManager fm;
    private ArrayList<Fragment> fragList = new ArrayList<Fragment>();

    private TextView titleTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

        View actionbarView = getLayoutInflater().inflate(R.layout.action_bar, null, false);
        getSupportActionBar().setCustomView(actionbarView);
        titleTv = actionbarView.findViewById(R.id.text_title);


        init();

        newFragment();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_home:
                main_home.setSelected(true);
                main_address.setSelected(false);
                main_find.setSelected(false);
                main_i.setSelected(false);
                showFragment(0);
                setActionBarTitle("首页");
//                Toast.makeText(this, "main_home", Toast.LENGTH_SHORT).show();
                break;
            case R.id.main_address:
                main_home.setSelected(false);
                main_address.setSelected(true);
                main_find.setSelected(false);
                main_i.setSelected(false);
                showFragment(1);
                setActionBarTitle("活动");
//                Toast.makeText(this, "main_address", Toast.LENGTH_SHORT).show();
                break;
            case R.id.main_find:
                main_home.setSelected(false);
                main_address.setSelected(false);
                main_find.setSelected(true);
                main_i.setSelected(false);
                showFragment(2);
                setActionBarTitle("发现");
//                Toast.makeText(this, "main_find", Toast.LENGTH_SHORT).show();
                break;
            case R.id.main_i:
                main_home.setSelected(false);
                main_address.setSelected(false);
                main_find.setSelected(false);
                main_i.setSelected(true);
                showFragment(3);
                setActionBarTitle("我");
//                Toast.makeText(this, "main_i", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void showFragment(int index) {
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        Fragment fragment = fragList.get(index);
        String tag = "";
        switch (index) {
            case 0:
                tag = "home";
                break;
            case 1:
                tag = "address";
                break;
            case 2:
                tag = "find";
                break;
            case 3:
                tag = "i";
                break;
        }
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.add(R.id.container, fragment, tag);
//            transaction.add(R.id.container, tag);
            transaction.show(fragment);
        }
        for (int i = 0; i < fragList.size(); i++) {
            if (index != i) {
                Fragment frg = fragList.get(i);
                if (frg.isAdded()) {
                    transaction.hide(frg);
                }
            }
        }

        // 提交事务
        transaction.commit();
    }

    private void newFragment() {
        homeFragment = new HomeFragment();
        addressFragment = new AddressFragment();
        findFragment = new ActivityFragment();
        iFragment = new IFragment();

        fragList.add(homeFragment);
        fragList.add(addressFragment);
        fragList.add(findFragment);
        fragList.add(iFragment);

        showFragment(0);
    }

    private void init() {
        main_home = findViewById(R.id.main_home);
        main_address = findViewById(R.id.main_address);
        main_find = findViewById(R.id.main_find);
        main_i = findViewById(R.id.main_i);

        main_home.setOnClickListener(this);
        main_address.setOnClickListener(this);
        main_find.setOnClickListener(this);
        main_i.setOnClickListener(this);

        main_home.setSelected(true);
        main_address.setSelected(false);
        main_find.setSelected(false);
        main_i.setSelected(false);
    }

    public void setActionBarTitle(String title) {
        titleTv.setText(title);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}